package com.aso.common.utils;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * Created by itsdf07 on 2017/7/19.
 */

public class WifiLocKManager {
    // 定义WifiManager对象
    private WifiManager mWifiManager;

    // 定义一个WifiLock
    private WifiManager.WifiLock mWifiLock;
    private Context context;

    public WifiLocKManager(Context context) {
        // 取得WifiManager对象
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        //第一种方式
        creatWifiLock("WifiLocKManager");
        //第二种方式
        //注意，在create wifilock的时候，一定要注意传WifiManager.WIFI_MODE_FULL_HIGH_PERF，否则的话在有些手机上貌似不起作用。不过估计对手机性能有所损耗
//        creatWifiLock("WifiLocKManager", WifiManager.WIFI_MODE_FULL_HIGH_PERF);
    }

    /**
     * 创建一个WifiLock
     *
     * @param locakName 名称
     * @param lockType  WIFI_MODE_FULL == 1 <br/>
     *                  扫描，自动的尝试去连接一个曾经配置过的点<br />
     *                  WIFI_MODE_SCAN_ONLY == 2 <br/>
     *                  只剩下扫描<br />
     *                  WIFI_MODE_FULL_HIGH_PERF = 3 <br/>
     *                  在第一种模式的基础上，保持最佳性能<br />
     */
    public void creatWifiLock(String locakName, int lockType) {
        mWifiLock = mWifiManager.createWifiLock(lockType, locakName);
    }

    /**
     * 创建一个WifiLock
     *
     * @param locakName 名称
     */
    public void creatWifiLock(String locakName) {
        mWifiLock = mWifiManager.createWifiLock(locakName);
    }

    /**
     * 锁定WifiLock
     */
    public void acquireWifiLock() {
        Log.d("dfsu", " acquireWifiLock : isHeld = " + mWifiLock.isHeld());
        if (mWifiLock.isHeld()) {
            return;
        }
        mWifiLock.acquire();
    }

    /**
     * 解锁WifiLock
     */
    public void releaseWifiLock() {
        // 判断时候锁定
        if (mWifiLock.isHeld()) {
            mWifiLock.release();
        }
    }

}
//<uses-permission android:name="android.permission.WAKE_LOCK"/>